import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from common.Utils import ForumDriver
from selenium.webdriver.common.by import By

class ForumDetail:
    driver = ""
    def __init__(self):
        self.driver = ForumDriver.driver

        #检查页面元素
    def ForumLoginSucDetail(self):
        #在登录成功后的论坛首页的前提下
        #点击一篇已经发布的帖子
        # element = self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(6) > div > div.col > div.text-truncate > a > strong")
        # self.driver.execute_script("arguments[0].scrollIntoView();", element)
        # element.click()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong").click()
        #进去帖子详情页后开始检查页面元素
        time.sleep(1)
        # #检查发布该篇帖子用户的头像
        # self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_avatar#article_details_author_avatar")
        #检查发布该篇帖子用户的昵称
        postpname = self.driver.find_element(By.CSS_SELECTOR,"#article_details_author_name").text
        print("发布帖子用户的昵称："+postpname)
        #检查一下帖子的标题
        postheadline = self.driver.find_element(By.CSS_SELECTOR,"#details_article_content_title").text
        print("帖子的标题："+ postheadline)
        #检查一下帖子的内容
        self.driver.find_element(By.CSS_SELECTOR,"#details_article_content")
        #该帖子有可能没有发布评论
        # #检查一下回复区列表
        # self.driver.find_element(By.CSS_SELECTOR,"#details_reply_area > div:nth-child(1) > div.col-9.card.card-lg > div.card-body")
        # #检查一下评论区列表头像
        # self.driver.find_element(By.CSS_SELECTOR,"#details_reply_area > div:nth-child(1) > div.col-3.card > div > span")
        #检查一下发送私信按钮

        self.driver.find_element(By.CSS_SELECTOR,"#btn_details_send_message > svg")
        #检查一下点赞按钮
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count")
        #检查一下回复区框
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")
        #检查一下回复按钮
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply")
        ForumDriver.GetScreeShot()
        time.sleep(2)

        # 测试页面功能
    def funPageFunctions(self):
        #测试一下点赞功能
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.jq-toast-wrap.bottom-right > div")
        ForumDriver.GetScreeShot()
        print("点赞成功")

        time.sleep(3)
        #测试一下空文本回复功能
        input_element = self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")
        # 创建 ActionChains 对象
        action = ActionChains(self.driver)
        # 输入文本并按下回车键
        action.click(input_element).send_keys("").send_keys(Keys.RETURN).perform()
        open = self.driver.find_element(By.CSS_SELECTOR, "#details_btn_article_reply > span")
        #检查一下，发表评论成功提醒
        ForumDriver.GetScreeShot()
        time.sleep(2)
        ForumDriver.GetScreeShot()
        print("存在BUG->发表空评论成功")


        time.sleep(3)
        # 测试一下非空文本回复功能
        # 创建 ActionChains 对象
        action = ActionChains(self.driver)
        # 输入文本并按下回车键
        action.click(input_element).send_keys("Hello, World!").send_keys(Keys.RETURN).perform()
        open = self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply > span")
        action.click(open)
        print("发表回复成功：")
        # 等待一段时间以便观察结果
        time.sleep(2)
        ForumDriver.GetScreeShot()
        # 检查一下，发表评论成功提醒
        time.sleep(1)



